package com.ustc.mvc.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ustc.mvc.action.Action;
import com.ustc.mvc.form.ActionForm;
import com.ustc.mvc.form.FullForm;
import com.ustc.mvc.util.XmlBean;

/**
 * Servlet implementation class ActionServlet
 */
@WebServlet("/ActionServlet")
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String path = request.getServletPath().split("\\.")[0];
		Map<String, XmlBean> map = (Map<String, XmlBean>)getServletContext().getAttribute("struts");
		ActionForm form = FullForm.fullForm(map.get(path).getBeanclass(), request);
		//System.out.println("name:"+name+"password:"+password);
		Action action = null;
		try {
			action = (Action) Class.forName(map.get(path).getActionclass()).newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String result = action.excute(request, form, map.get(path).getForwardmap());
		
		request.getRequestDispatcher(result).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
